//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html

import ResKeeper from './ResKeeper';
const { ccclass, property } = cc._decorator;

/**
 * 建议类名: ClickButtonManager
 * AI 控制类，管理与 AI 点击按钮相关的逻辑
 */
@ccclass
class AICtrl extends ResKeeper {
    /** 鼠标点节点 */
    @property(cc.Node)
    public shuBiaoDian: cc.Node = null;

    /** 阻挡节点 */
    @property(cc.Node)
    public blockNode: cc.Node = null;

    /** 是否开启 */
    public isOpen: boolean = false;

    /** AI冷却时间 */
    public aiCDTime: number = 0.5;

    /** 点击冷却时间 */
    public clickCDTime: number = 1;

    /** 鼠标延迟时间 */
    public mouseDelayTime: number = 0.5;

    /** 拖拽时间 */
    public dragTime: number = 1;

    /** 开始延迟时间 */
    public startDelayTime: number = 2;

    /** 战斗延迟时间 */
    public fightDelayTime: number = 1;

    /** 是否是AI时间 */
    public isAiTime: boolean = false;

    public static _instance: AICtrl;

    /**
     * 建议方法名: initialize
     * 加载时调用的方法
     */
    public onLoad(): void {
        AICtrl._instance = this;
        this.node.zIndex = 9999;
        this.blockNode.active = false;
    }

    /**
     * 建议方法名: displayClickButton
     * 显示点击按钮
     * @param targetNode 事件对象
     * @param clickCallback 回调函数
     */
    public displayClickButton(targetNode: cc.Node, clickCallback?: Function): void {
        if (this.isOpen && !this.isAiTime) {
            this.isAiTime = true;
            this.scheduleOnce(() => {
                this.shuBiaoDian.active = true;
                this.shuBiaoDian.stopAllActions();
                const worldPosition = targetNode.convertToWorldSpaceAR(cc.v2(0, 0));
                const localPosition = this.shuBiaoDian.parent.convertToNodeSpaceAR(worldPosition);
                this.shuBiaoDian.setPosition(localPosition);

                this.shuBiaoDian.runAction(
                    cc.sequence(
                        cc.delayTime(this.mouseDelayTime),
                        cc.callFunc(() => {
                            this.shuBiaoDian.active = false;
                            this.isAiTime = false;
                            if (clickCallback) {
                                clickCallback();
                            }
                        })
                    )
                );
            }, this.clickCDTime);
        }
    }
}

export default AICtrl;